<template>
	<div class="system-role-dialog-container energy-from energy-dialog">
		<el-dialog center align-center v-model="state.dialog.isShowDialog" width="769px" @close="resetData">
			<template #header="{ titleClass }">
				<div :class="titleClass">{{ state.dialog.isAdd ? '新增租户' : '修改租户' }}</div>
			</template>
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="租户名" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入租户名" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="联系人" prop="contactName">
							<el-input v-model="state.ruleForm.contactName" placeholder="请输入联系人" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="手机号" prop="contactMobile">
							<el-input v-model="state.ruleForm.contactMobile" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="绑定域名" prop="website">
							<el-input v-model="state.ruleForm.website" placeholder="请输入绑定域名" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="账号数量" prop="accountCount">
							<el-input-number v-model="state.ruleForm.accountCount" :min="1" :max="10" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20" v-if="state.dialog.isAdd">
						<el-form-item label="管理员密码" prop="password">
							<el-input v-model="state.ruleForm.password" placeholder="请输入管理员密码" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div class="flex row-center mt10 mb10">
				<div class="btn3" @click="onCancel">
					<el-icon> <ele-Close /> </el-icon><span class="ml6">取 消</span>
				</div>
				<div class="btn1 ml10" @click="onSubmit">
					<el-icon> <ele-Check /> </el-icon><span class="ml6">保 存</span>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useTenantApi } from '/@/api/system/tenant/index';
import { ElNotification } from 'element-plus';
import JSEncrypt from 'jsencrypt';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const roleDialogFormRef = ref();
const state: any = reactive({
	ruleForm: {
		// name: '', // 租户名
		// contactName: '', // 联系人
		// contactMobile: '', // 联系手机 用于创建管理员账号
		// website: '', // 绑定域名
		// accountCount: 1, // 账号数量
		// password: '', // 管理员密码
	},
	dialog: {
		isShowDialog: false,
		isAdd: true,
	},
	rules: {
		name: [{ required: true, message: '租户名不能为空', trigger: 'blur' }],
		contactName: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
		contactMobile: [
			{ required: true, message: '请输入用户手机号', trigger: 'blur' },
			{ pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' },
		],
		website: [{ required: true, message: '绑定域名不能为空', trigger: 'blur' }],
		accountCount: [{ required: true, message: '账号数量不能为空', trigger: 'blur' }],
		password: [{ required: true, message: '管理员密码不能为空', trigger: 'blur' }],
	},
});

// 打开弹窗
const openDialog = (row: any) => {
	if (row) {
		state.ruleForm = {
			id: row.id,
			name: row.name, // 角色名称
			contactName: row.contactName, // 联系人
			contactMobile: row.contactMobile, // 联系手机 用于创建管理员账号
			website: row.website, // 绑定域名
			accountCount: row.accountCount, // 账号数量
			adminUserId: row.adminUserId, // 管理员用户id
		};
		state.dialog.isAdd = false;
	} else {
		state.dialog.isAdd = true;
	}

	state.dialog.isShowDialog = true;
};

// 新增租户
const handleAddTenant = async () => {
	const crypt = new JSEncrypt();
	crypt.setKey(import.meta.env.VITE_PUBLIC_kEY);
	state.ruleForm.password = crypt.encrypt(state.ruleForm.password);
	const data = await useTenantApi().addTenant(state.ruleForm);
	if (data.code == 200) {
		emit('refresh');
		ElNotification({
			title: '提示',
			message: '新增租户成功',
			position: 'top-right',
			type: 'success',
		});
	}
};

// 编辑租户
const handleEditTenant = async () => {
	const data = await useTenantApi().updateTenant(state.ruleForm);
	if (data.code == 200) {
		emit('refresh');
		ElNotification({
			title: '提示',
			message: '修改租户成功',
			position: 'top-right',
			type: 'success',
		});
	}
};

// 提交
const onSubmit = () => {
	roleDialogFormRef.value.validate((valid: any) => {
		if (valid) {
			state.dialog.isAdd ? handleAddTenant() : handleEditTenant();
		} else {
			ElNotification({
				title: '提示',
				message: '校验失败,请填写完整租户信息',
				position: 'top-right',
				type: 'error',
			});
		}
		closeDialog();
	});
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};

// 表单清空
const resetData = () => {
	roleDialogFormRef.value.resetFields();
	state.ruleForm = {
		id: '',
		name: '', // 租户名
		contactName: '', // 联系人
		contactMobile: '', // 联系手机 用于创建管理员账号
		website: '', // 绑定域名
		accountCount: 0, // 账号数量
		password: '', // 管理员密码
	};
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
